package com.zmn.brs.task.job;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.brs.business.interfaces.objective.ObjectiveTargetBService;
import com.zmn.brs.business.interfaces.objective.ObjectiveTargetJobBService;
import com.zmn.brs.common.constant.TaskConst;
import com.zmn.brs.model.entity.scene.SceneQuery;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 类描述：目标监控规则定时任务
 * @author chongw
 * @since 2021/06/09 11:12
 */
@LTS
@Slf4j
public class ObjectiveRuleJob {

    private static final String TAG = "目标绩效监控规则定时任务触发类";

    @NacosValue(value = "${sms.ruleId.dingTalk}", autoRefreshed = true)
    Integer smsRuleId;

    @NacosValue(value = "${spring.mq.topic.push}", autoRefreshed = true)
    String pushTopic;

    @Resource
    private TaskManager taskManager;

    @Resource
    private ObjectiveTargetBService objectiveTargetBService;

    @Resource
    private ObjectiveTargetJobBService objectiveTargetJobBService;

    @PostConstruct
    public void init() {
        log.info("[{}], 部门目标规则定时任务初始化提交", TAG);
        //初始化任务
        objectiveTargetJobBService.initJob(SceneQuery.builder().build());
        Job job = new Job();
        job.setTaskId(TaskConst.TASK_ID_BRS_DATA_PREPARATION);
        job.setCronExpression("0 40 23 * * ?");
        job.setReplaceOnExist(true);
        job.setParam(TaskManager.SHARD_FIELD, TaskConst.TASK_ID_BRS_DATA_PREPARATION);
        taskManager.submitTask2(job);
        log.info("[{}], 提交每天23点40分规则任务初始化成功......", TAG);
    }

    /**
     * 描述: 目标监控推送任务初始化
     * @param job 任务
     * @return com.github.ltsopensource.tasktracker.Result
     * @author chongw
     * @since 2021/06/11 11:12
     */
    @JobRunnerItem(shardValue = TaskConst.TASK_ID_BRS_DATA_PREPARATION)
    public Result preJob(Job job) {
        try {
            objectiveTargetJobBService.initJob(SceneQuery.builder().build());
            return new Result(Action.EXECUTE_SUCCESS, "目标监控推送任务初始化成功");
        }catch (Exception e){
            log.error("[{}], #JOB#TASK_ID_BRS_DATA_PREPARATION#  目标监控推送任务初始化失败 failed reason: {}", TAG, e.getMessage(), e);
            return new Result(Action.EXECUTE_FAILED, " 目标监控推送任务初始化失败");
        }
    }


    /**
     * 描述: 目标监控推送任务
     * @param job 任务
     * @return com.github.ltsopensource.tasktracker.Result
     * @author chongw
     * @since 2021/06/09 11:12
     */
    @JobRunnerItem(shardValue = TaskConst.TASK_ID_BRS_SCENE)
    public Result computeJob(Job job) {
        try {
            //开始执行任务，如果推送周期为每周，则实时推送
            Boolean result = objectiveTargetBService.computeObjectiveTask(job);
            if (result) {
                return new Result(Action.EXECUTE_SUCCESS, "指标计算任务成功");
            }
            log.error("[{}], #JOB#TASK_ID_BRS_SCENE_COMPUTE#  指标计算任务，执行失败 {}", TAG);
            return new Result(Action.EXECUTE_FAILED, " 指标计算任务");
        } catch (Exception e) {
            log.error("[{}], #JOB#TASK_ID_BRS_SCENE_COMPUTE#  指标计算任务失败 failed reason: {}", TAG, e.getMessage(), e);
            return new Result(Action.EXECUTE_FAILED, " 指标计算任务失败");
        }
    }

    /**
     * 描述: 目标监控推送任务
     * @param job 任务
     * @return com.github.ltsopensource.tasktracker.Result
     * @author chongw
     * @since 2021/06/09 11:12
     */
    @JobRunnerItem(shardValue = TaskConst.TASK_ID_BRS_SCENE_PUSH)
    public Result pushJob(Job job) {
        try {
            //开始执行任务，如果推送周期为每周，则实时推送
            Boolean result = objectiveTargetBService.pushObjectiveTask(job,smsRuleId,pushTopic);
            if (result) {
                return new Result(Action.EXECUTE_SUCCESS, "指标推送任务成功");
            }
            log.error("[{}], #JOB#TASK_ID_BRS_SCENE_PUSH#  指标推送任务，执行失败 {}", TAG);
            return new Result(Action.EXECUTE_FAILED, " 指标推送任务");
        } catch (Exception e) {
            log.error("[{}], #JOB#TASK_ID_BRS_SCENE_PUSH#  指标推送任务失败 failed reason: {}", TAG, e.getMessage(), e);
            return new Result(Action.EXECUTE_FAILED, " 指标推送任务失败");
        }
    }

}
